package cn.edu.ujn.booking.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booking.dao.Dict;
import cn.edu.ujn.booking.dao.Drama;
import cn.edu.ujn.booking.dao.User;
import cn.edu.ujn.booking.service.DictService;
import cn.edu.ujn.booking.service.DramaService;
import cn.edu.ujn.booking.service.UserService;


@Controller
public class LoginController {
	@Autowired
	private UserService userService;
	@Autowired
	private DramaService dramaService;
	@Autowired
	private DictService dictService;
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String toLogin(String username, String password, String submitType, Model model, HttpSession session) {
//		System.out.println(username+" "+password+" "+submitType);
		List<Drama> dramas = dramaService.findAll();
		if ("登录".equals(submitType)) {
			User user = userService.findUser(username,password);
			//System.out.println("查询结果"+user);
			session.setAttribute("USER_SESSION", user);
			
			//System.out.println(dramas);
			if (user != null && user.getIdentity().equals("普通用户")) {	
				session.setAttribute("user_dramas", dramas);
				List<Dict> dict = dictService.findAllDict();
				//System.out.println(dict);
				session.setAttribute("dramas_seat", dict);
				return "home";
			}
			else if(user != null && (user.getIdentity().equals("工作人员") || user.getIdentity().equals("管理员") )){
				session.setAttribute("dramas", dramas);
				List<User> user2 = userService.findUserByIdentity("工作人员");
				session.setAttribute("sys_user", user2);
				List<Dict> detail = dictService.detail();
				session.setAttribute("detail", detail);
				System.out.println(detail);
				return "redirect:backstage";
			}
			model.addAttribute("msg", "账号或密码错误，请重新输入！");
			return "login";
		} else {
			return "register";
		}
	}
	@RequestMapping("/third/add.action")

	public String toAdd(String identity1,User user,HttpSession session) {
		String message="";
		if(identity1.equals("管理员")) {
			user.setIdentity("工作人员");
			User addUser = userService.addUser(user.getUsername(), user.getPassword(), user.getIdentity());
			System.out.println(addUser);
			
			if(addUser!=null) {
				message="添加账号成功";
			}else {
				message="添加账号失败";
				
			}
		}
		else{
			message="对不起，你没有权限添加账号"; 
		}
		session.setAttribute("addmsg", message);
		return "backstage";
	}
	@RequestMapping("/toregister")
	public String toRegister2() {
		return "register";
	}
	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	public String toRegister(String username,String password,Model model) {
		if (userService.findUserByUsername(username) != null) {
			model.addAttribute("msg2", "已有该账号，请点击登录按钮！");
			return "login";
		}
		String identity="普通用户";
		User addUser = userService.addUser(username, password,identity);
		if (addUser != null) {
			System.out.println(addUser);
			model.addAttribute("msg", "注册成功，欢迎登录！");
			return "login";
		}
		model.addAttribute("msg2", "注册失败，请重新注册！");
		// 返回到登录页面
		return "login";
	}
	@RequestMapping("/third/delete1.action")
	@ResponseBody
	public String toDel(int id,HttpSession session) {
		System.out.println(id);
		int rows = userService.deleteUser(id);
		List<User> user2 = userService.findUserByIdentity("工作人员");
		System.out.println(user2);
		session.setAttribute("sys_user", user2);
		if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
	@RequestMapping("/index")
	public String toIndex() {
		return "login";
	}
	@RequestMapping("/home")
	public String toHome() {
		return "home";
	}
	@RequestMapping("/backstage")
	public String toBack() {
		return "backstage";
	}
	@RequestMapping(value = "/addUser.action", method = RequestMethod.POST)
	public String toAddUser(String username,String password,HttpSession session) {
		String identity="工作人员";
		User user = userService.addUser(username, password, identity);
		List<User> user2 = userService.findUserByIdentity("工作人员");
		session.setAttribute("sys_user", user2);
		return "backstage";
		
	}
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin2() {
		return "login";
	}
}
